const express = require('express');
const path = require('path');
const webpack = require('webpack');
const consoler = require('consoler');
const devMiddleware = require('webpack-dev-middleware');
const hotMiddleware = require('webpack-hot-middleware');
const {
    webpackDevConfig,
    DEV_SERVER_CONFIG,
} = require('./config/webpack.dev.js');

const app = express();
const compiler = webpack(webpackDevConfig);

app.use(express.static(path.resolve(__dirname, '../public/dist')));
app.use(devMiddleware(compiler, {
    writeToDisk: (filePath) => filePath.endsWith('.tpl'),
    publicPath: webpackDevConfig.output.publicPath,
    headers: {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
        'Access-Control-Allow-Headers': 'X-Requested-With, content-type, Authorization',
    },
    stats: {
        colors: true,
    }
}));
app.use(hotMiddleware(compiler, {
    path: `/${DEV_SERVER_CONFIG.HMR_PATH}`,
    log: () => {}
}));


consoler.info('请等待初次构建中');

const port = DEV_SERVER_CONFIG.PORT;

app.listen(port, () => {
    consoler.success(`开发服务器启动成功，端口号：${port}`);
});
